package cn.edu.sdjzu.xg23.kcsj01_backend.controller;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Conference;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.impl.ConferenceServiceImpl;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/conference")
public class ConferenceController {

    @Autowired
    private ConferenceServiceImpl conferenceServiceImpl;

    @PostMapping
    public Result save(@RequestBody Conference conference) {
        boolean isSuccess = conferenceServiceImpl.save(conference);
        if (isSuccess) {
            return Result.success("保存成功");
        }
        return Result.error("010", "保存失败");
    }

    @PutMapping
    public Result update(@RequestBody Conference conference) {
        boolean isSuccess = conferenceServiceImpl.update(conference);
        if (isSuccess) {
            return Result.success("更新成功");
        }
        return Result.error("012", "更新失败");
    }

    @DeleteMapping("/{conferenceId}")
    public Result delete(@PathVariable Long conferenceId) {
        boolean isSuccess = conferenceServiceImpl.delete(conferenceId);
        if (isSuccess) {
            return Result.success("删除成功");
        }
        return Result.error("011", "删除失败");
    }

    @GetMapping
    public Result get(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @ModelAttribute Conference conference
    ) {
        PageInfo<Conference> pageInfo = conferenceServiceImpl.get(pageNum, pageSize, conference);
        return Result.success(pageInfo);
    }
}